package com.tensquare.article.controller;

import com.tensquare.article.pojo.Comment;
import com.tensquare.article.service.CommentService;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 添加评论
     * @param comment
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Comment comment){
        // 调用业务添加
        commentService.add(comment);
        return new Result(true, StatusCode.OK,"添加成功");
    }

    /**
     * 查询所有
     */
    @GetMapping
    public Result findAll(){
        List<Comment> list = commentService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",list);
    }

    /**
     * 通过编号查询
     * @param commentId
     */
    @GetMapping("/{commentId}")
    public Result findById(@PathVariable String commentId){
        // 调用业务查询
        Comment comment = commentService.findById(commentId);
        return new Result(true, StatusCode.OK,"查询成功",comment);
    }

    /**
     * 修改
     */
    @PutMapping("/{commentId}")
    public Result update(@PathVariable String commentId,@RequestBody Comment comment){
        // 设置修改的id
        comment.set_id(commentId);
        // 调用业务更新
        commentService.update(comment);
        return new Result(true, StatusCode.OK,"更新成功");
    }

    /**
     * 删除
     */
    @DeleteMapping("/{commentId}")
    public Result deleteById(@PathVariable String commentId){
        // 调用业务删除
        commentService.deleteById(commentId);
        return new Result(true, StatusCode.OK,"删除成功");
    }

    /**
     *  根据文章id查询评论
     */
    @GetMapping("/article/{articleId}")
    public Result findByArticleId(@PathVariable String articleId){
        // 调用业查询
        List<Comment> comments = commentService.findByArticleId(articleId);
        return new Result(true, StatusCode.OK,"查询成功",comments);
    }

    /**
     * 点赞
     * @param loginUserId 这个参数是为了测试用的，真实开发下是用户的id是可以通过token获取的
     */
    @PutMapping("/thumbup/{commentId}")
    public Result thumbup(@PathVariable String commentId, @RequestParam String loginUserId){
        // 调用业务实现点赞
        commentService.thumbup(commentId,loginUserId);
        return new Result(true, StatusCode.OK,"点赞成功");
    }

}
